#!/bin/bash

# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

SDK_URL="https://cidownload.openharmony.cn/version/Master_Version/OpenHarmony_5.0.2.43/20241008_020250/version-Master_Version-OpenHarmony_5.0.2.43-20241008_020250-ohos-sdk-full.tar.gz"

NEW_OHOS_SDK=new-os-sdk
OHOS_SDK=os-sdk

function prepare_harmonyos_sdk(){
    if [ ! -d "new-os-sdk12" ]
    then
        git clone https://gitee.com/lihui868/new-os-sdk12.git
        if [ $? -ne 0 ]
        then
            echo "clone new-os-sdk12 failed!"
            rm -rf new-os-sdk12
            return 1
        fi
    fi

    return 0
}

function prepare_hos_sdk_api() {
    echo "prepare_hos_sdk_api"
	
    # 下载 HarmonyOS SDK
    prepare_harmonyos_sdk
	if [ $? -ne 0 ]
    then
        echo "prepare_harmonyos_sdk failed!!"
        return 1
    fi
	
    sdk_last=ohos_sdk_20240820_020224.tar.gz
    sdk_name=ohos_sdk_20241008_020250.tar.gz

    if [ -f "$sdk_last" ]; then
      rm $sdk_last
    fi  
	
    if [ -f "$sdk_name" ]; then
        echo "File $sdk_name exists, do not download again."
    else
        echo "Start downloading $sdk_name"
        curl -o $sdk_name $SDK_URL
    fi
    if [ $? != 0 ]; then
        echo "Download $sdk_name failed."
        rm -rf $sdk_name
    fi

    echo "Start unzip ohos_sdk."
    rm -rf $NEW_OHOS_SDK && rm -rf $OHOS_SDK
    mkdir $NEW_OHOS_SDK
    mkdir $OHOS_SDK
    tar -xf $sdk_name -C $OHOS_SDK/
    mkdir -p ohos-sdk/openharmony && find $OHOS_SDK/linux/ -name "*.zip" -exec unzip -q {} -d $OHOS_SDK/openharmony/ \;
    cp -r new-os-sdk12/HarmonyOS-NEXT-DB1 $NEW_OHOS_SDK/HarmonyOS-NEXT-DB1
    mv $OHOS_SDK/openharmony $NEW_OHOS_SDK/HarmonyOS-NEXT-DB1/
    echo "Finish unzip ohos_sdk."
}

function prepare(){
	prepare_hos_sdk_api
}

prepare $@

exit $?

#eof
